//
//  PywGetAccountListApi.m
//  PywSdk
//
//  Created by smallwave on 16/8/26.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywGetAccountListApi.h"
#import "AIFGetAccountListManager.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"
#import "ETArrayReformer.h"

@interface PywGetAccountListApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywGetAccountListApi

- (void)requestGetAccountListWithSuccess:(Successd)success
                                    fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    
    AIFGetAccountListManager *manager = [AIFGetAccountListManager manager];
    manager.delegate = self;
    [manager loadData];
    
}
#pragma mark - ETAPIManagerApiCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    
    [self getAccountListWithDict:dict];
}
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywMBProgressHUD showError:@"获取账号管理列表失败！" toView:[UIApplication sharedApplication].keyWindow];
    [self getAccountListFail];
}
/**
 *  获取游戏账号列表成功
 */
- (void)getAccountListWithDict:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    switch (resultCode) {
        case 200:
        {
            NSArray *data = [dict objectForKey:@"data"];
            if (self.successd) {
                self.successd(nil,data);
            }
        }
            break;
            
        default:
        {
            NSString *errorMsg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:errorMsg toView:[UIApplication sharedApplication].keyWindow];
            if (self.failure) {
                self.failure([self errorWithCode:resultCode]);
            }
        }
            break;
    }

}
/**
 *  获取游戏账号列表失败
 */
- (void)getAccountListFail{
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

@end
